package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.VxCheckFaceAddDTO;
import org.jsola.hr.dto.VxCheckFaceUpdateDTO;
import org.jsola.hr.query.VxCheckFaceQuery;
import org.jsola.hr.service.IVxCheckFaceService;
import org.jsola.hr.vo.VxCheckFaceListVO;
import org.jsola.hr.vo.VxCheckFaceVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 微信人脸核身
 *
 * @author zhr
 */
@RestController("hrVxCheckFaceController")
@RequestMapping(HrConstants.API_PATH + "/v1/vxCheckFaces")
public class VxCheckFaceController implements IVxCheckFaceControllerAPI {

    @Autowired
    private IVxCheckFaceService vxCheckFaceService;

    @Override
    @PostMapping
    public Result<VxCheckFaceVO> save(@Valid @RequestBody VxCheckFaceAddDTO vxCheckFaceAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增微信人脸核身权限校验

        return Result.success(
                vxCheckFaceService.save(vxCheckFaceAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody VxCheckFaceUpdateDTO vxCheckFaceUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改微信人脸核身权限校验

        int count = vxCheckFaceService.update(vxCheckFaceUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{vxCheckFaceId}")
    public Result<Integer> delete(@PathVariable String vxCheckFaceId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除微信人脸核身权限校验

        int count = vxCheckFaceService.deleteByIds(tokenUser, vxCheckFaceId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] vxCheckFaceIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除微信人脸核身权限校验

        int count = vxCheckFaceService.deleteByIds(tokenUser, vxCheckFaceIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{vxCheckFaceId}")
    public Result<VxCheckFaceVO> findById(@PathVariable String vxCheckFaceId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看微信人脸核身权限校验

        return Result.success(
            vxCheckFaceService.selectById(vxCheckFaceId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<VxCheckFaceListVO>> listAll(@Valid VxCheckFaceQuery vxCheckFaceQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看微信人脸核身权限校验

        return Result.success(
            vxCheckFaceService.select(vxCheckFaceQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<VxCheckFaceListVO>> page(@Valid VxCheckFaceQuery vxCheckFaceQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            vxCheckFaceService.selectPage(vxCheckFaceQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/startCheckFace")
    public Result<VxCheckFaceVO> startCheckFace(@RequestBody VxCheckFaceAddDTO vxCheckFaceAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                vxCheckFaceService.startCheckFace(vxCheckFaceAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping("/startCheckFace/{companyId}")
    public Result<VxCheckFaceVO> startCheckFaceByCompanyId(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                vxCheckFaceService.startCheckFaceByCompanyId(companyId, tokenUser)
        );
    }



}

